<?php
class job_announcement extends Zcontroller{
	
    function __construct() {
   	    parent::__construct();
   	    //menu current page
		Zsession::setDataToSession("current_page", "job");		
    }
	
	public function index() {  
		echo "Job Announcement Page";	
    }
    
    public function post(){
    	$Z_USER=Zsession::getUserFromSession();
    	if($Z_USER["user_id"]!=null && $Z_USER["user_id"]!="" ){
    		$this->load->model("job_announcement_model");
    		$this->load->template("templates/general","job_announcement/general_information","Post Job Announcement");
    	}else{
    		redirect(site_url("users"));
    	}
    }
    
 	function submit_general_information(){
 		$Z_USER = Zsession::getUserFromSession();
 		if(isset($Z_USER["user_id"])&& $Z_USER["user_id"]!=""){
	    	$this->load->model("job_announcement_model");
	    	$this->load->helper('email');
	        $data =array();
	        $name=trim($_POST['txtorgname']);
	        $desc=trim($_POST['txtdesc']);
	        $address=trim($_POST['txtaddress']);
	        $phone=trim($_POST['txtphone']);
	        $fax=trim($_POST['txtfax']);
	        $email=trim($_POST['txtemail']);
	        $contact=trim($_POST['txtcontact']);
	        $website=trim($_POST['txtwebsite']);
	        
	        $notice=trim($_POST["txtappinfo"]);
	 		$closingdate=$_POST["txtclosingdate"];
	            /* (pretty self-explanatory) */
			      $config = array();
			      $project_path = str_replace("\\", "/", FCPATH);
			      $config['upload_path'] = $project_path.'/images/company_logo/'; /* NB! create this dir! */
			     // $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
			      $config['max_size']  = '0';
			      $config['max_width']  = '0';
			      $config['max_height']  = '0';
			      /* Load the upload library */
			      $this->load->library('upload', $config);
			      
			      /* Create the config for image library */
			      /* (pretty self-explanatory) */
			      $configThumb = array();
			      $configThumb['image_library'] = 'gd2';
			      $configThumb['source_image'] = '';
			      $configThumb['create_thumb'] = TRUE;
			      $configThumb['maintain_ratio'] = TRUE;
			      $configThumb['width'] = 140;
			      $configThumb['height'] = 210;
			      /* Load the image library */
			      $this->load->library('image_lib');
				  $logotype="";
				  $logoname="";
				  $filesize=0;
			 	 if(isset($_FILES["imagefile"])&& $_FILES["imagefile"]['type']!=""){
			 	  		$logotype=$_FILES['imagefile']['type'];
			 			$ext = $_FILES['imagefile']['type'] ;
			 			$filesize=$_FILES["imagefile"]["size"];
			          	$curdt=date('m/d/Y h:i:s a', time());
			          	$encryptedname=Zencryption::encrypt($filesize.$curdt);
			          	$filename=$_FILES['imagefile']['name'];
			       		$ext =end(explode(".", $filename));
			       		$logoname=$encryptedname.".".$ext;
			       		$target=$project_path.'/images/company_logo/'.$logoname;
			       		move_uploaded_file($_FILES['imagefile']['tmp_name'],$target);
			     }
			     /* Insert New Company */
	       		$this->job_announcement_model->insertCompany($name,$desc,$address,$phone,$fax,$email,$contact,$website,$filesize,$logoname,$logotype,$Z_USER["user_id"]);
				/* Get Max ID of Company By Current User */
				$comid=$this->job_announcement_model->selectMaxCompanyID();
				/* Insert Announcement */
	       		$this->job_announcement_model->insertAnnouncement($notice,$closingdate,$comid);
				/* Get Max ID of Announcement By Current User */
	       		$annid=$this->job_announcement_model->selectMaxAnnounID();
				/* Insert Job Titles */
		 		if (isset($_POST["positions"]) && is_array($_POST["positions"])&& count($_POST["positions"])> 0) 
				{ 
				    foreach ($_POST["positions"] as $position) 
				        { 
							if(trim($position)!=""){
								$this->job_announcement_model->insertTitle($annid,$position);
							}
				        } 
				}

	       		redirect(site_url("job_announcement/post_requirements_and_duties"."/".$annid));
 	   }else{
    		redirect(site_url("users"));
    	}
 	}
 	
 	public function post_requirements_and_duties($announ_id){
 		$this->load->model("job_announcement_model");
 		$Z_USER=Zsession::getUserFromSession();
    	if(isset($Z_USER["user_id"])){
	 		$data["titles"]=$this->job_announcement_model->selectTitleByAnnounID($announ_id);
	 		$data["categories"]=$this->job_announcement_model->selectCategories();
	 		$data["locations"]=$this->job_announcement_model->selectLocations();
	 		$data["schedules"]=$this->job_announcement_model->selectSchedules();
	 		$this->load->template("templates/general","job_announcement/requirements_and_duties","Post Job Announcement",$data);
    	}else{
    		redirect(site_url("users"));
    	}
 	}
 	public function add_requirement(){
 		$this->load->model("job_announcement_model");
 		$title_id=$_GET["titleid"];
 		$req_desc=$_GET["req"];
 		$order=$_GET["order"];
 		$this->job_announcement_model->insertRequirement($title_id,$req_desc);
 		$table_header="<table class=\"requirement\" cellpadding=\"0\" cellspacing=\"0\" width=\"90%\">".
					  "<tr id=\"header\" align=\"center\" height=\"20\">".
							"<td id=\"title1\" height=\"25\">Requirements</td>".
							"<td id=\"title2\" width=\"20%\">Delete</td></tr>";
		$table_body="";
 		$requirements=$this->job_announcement_model->selectRequirementsByTitleID($title_id);
 		foreach($requirements->result_array() as $requirement){
 			$table_body.="<tr id=\"body\">".
 							"<td height=\"25\" id=\"column1\">".htmlspecialchars($requirement["req_desc"])."</td>".
 							"<td id=\"column2\"><img width=\"12\" height=\"12\" src=\"".base_url()."images/DeleteRed.png\" style=\"cursor: pointer; color:#0085CF;\" onclick=\"javascript:removeRequirement('".$requirement["req_id"]."','".$title_id."','".$order."','".site_url("job_announcement/delete_requirement")."')\"/></td>".
 						  "</tr>";
 		}				
		$table_footer="</table>";
		$table=$table_header.$table_body.$table_footer;
		echo $table;
 	}
 	public function delete_requirement(){
 		$this->load->model("job_announcement_model");
 		$title_id=$_GET["titleid"];
 		$req_id=$_GET["req_id"];
 		$order=$_GET["order"];
 		$this->job_announcement_model->deleteRequirement($req_id);
 		$table_header="<table class=\"requirement\" cellpadding=\"0\" cellspacing=\"0\" width=\"90%\">".
					  "<tr id=\"header\" align=\"center\" height=\"20\">".
							"<td id=\"title1\" height=\"25\">Requirements</td>".
							"<td id=\"title2\" width=\"20%\">Delete</td></tr>";
		$table_body="";
 		$requirements=$this->job_announcement_model->selectRequirementsByTitleID($title_id);
 		foreach($requirements->result_array() as $requirement){
 			$table_body.="<tr id=\"body\">".
 							"<td height=\"25\" id=\"column1\">".htmlspecialchars($requirement["req_desc"])."</td>".
 							"<td id=\"column2\"><img width=\"12\" height=\"12\" src=\"".base_url()."images/DeleteRed.png\" style=\"cursor: pointer; color:#0085CF;\" onclick=\"javascript:removeRequirement('".$requirement["req_id"]."','".$title_id."','".$order."','".site_url("job_announcement/delete_requirement")."')\" /></td>".
 						  "</tr>";
 		}				
		$table_footer="</table>";
		$table=$table_header.$table_body.$table_footer;
		echo $table;
 	}
 	public function add_duty(){
 		$this->load->model("job_announcement_model");
 		$title_id=$_GET["titleid"];
 		$duty_desc=$_GET["duty"];
 		$order=$_GET["order"];
 		$this->job_announcement_model->insertDuty($title_id,$duty_desc);
 		$table_header="<table class=\"requirement\" cellpadding=\"0\" cellspacing=\"0\" width=\"90%\">".
					  "<tr id=\"header\" align=\"center\" height=\"20\">".
							"<td id=\"title1\" height=\"25\">Duties</td>".
							"<td id=\"title2\" width=\"20%\">Delete</td></tr>";
		$table_body="";
 		$duties=$this->job_announcement_model->selectDutiesByTitleID($title_id);
 		foreach($duties->result_array() as $duty){
 			$table_body.="<tr id=\"body\">".
 							"<td height=\"25\" id=\"column1\">".htmlspecialchars($duty["duty_desc"])."</td>".
 							"<td id=\"column2\"><img width=\"12\" height=\"12\" src=\"".base_url()."images/DeleteRed.png\" style=\"cursor: pointer; color:#0085CF;\" onclick=\"javascript:removeDuty('".$duty["duty_id"]."','".$title_id."','".$order."','".site_url("job_announcement/delete_duty")."')\"/></td>".
 						  "</tr>";
 		}				
		$table_footer="</table>";
		$table=$table_header.$table_body.$table_footer;
		echo $table;
 	}
 	
 	public function delete_duty(){
 		$this->load->model("job_announcement_model");
 		$title_id=$_GET["titleid"];
 		$duty_id=$_GET["duty_id"];
 		$order=$_GET["order"];
 		$this->job_announcement_model->deleteDuty($duty_id);
 		$table_header="<table class=\"requirement\" cellpadding=\"0\" cellspacing=\"0\" width=\"90%\">".
					  "<tr id=\"header\" align=\"center\" height=\"20\">".
							"<td id=\"title1\" height=\"25\">Duties</td>".
							"<td id=\"title2\" width=\"20%\">Delete</td></tr>";
		$table_body="";
 		$duties=$this->job_announcement_model->selectDutiesByTitleID($title_id);
 		foreach($duties->result_array() as $duty){
 			$table_body.="<tr id=\"body\">".
 							"<td height=\"25\" id=\"column1\">".htmlspecialchars($duty["duty_desc"])."</td>".
 							"<td id=\"column2\"><img width=\"12\" height=\"12\" src=\"".base_url()."images/DeleteRed.png\" style=\"cursor: pointer; color:#0085CF;\" onclick=\"javascript:removeDuty('".$duty["duty_id"]."','".$title_id."','".$order."','".site_url("job_announcement/delete_duty")."')\"/></td>".
 						  "</tr>";
 		}					
		$table_footer="</table>";
		$table=$table_header.$table_body.$table_footer;
		echo $table;
 	}
 	
 	public function complete(){
 		$this->load->model("job_announcement_model");
 		$count=$_POST["number_of_titles"];
 		for($i=1;$i<=$count;$i++){
 			$this->job_announcement_model->insertCategory($_POST["title".$i],$_POST["cat".$i]);
 			$this->job_announcement_model->insertLocation($_POST["title".$i],$_POST["loc".$i]);
 			$this->job_announcement_model->insertSchedule($_POST["title".$i],$_POST["sched".$i]);
 		}
 		redirect(site_url("job_announcement/job_list"));
 	}	
 	
 	public function edit_announcement($ann_id){
 		$this->load->model("job_announcement_model");
 		$Z_USER=Zsession::getUserFromSession();
    	if(isset($Z_USER["user_id"])){
	 		$data["announcement"]=$this->job_announcement_model->selectAnnouncement($ann_id);
	 		$data["titles"]=$this->job_announcement_model->selectTitleByAnnounID($ann_id);
	 		$data["categories"]=$this->job_announcement_model->selectCategories();
	 		$data["locations"]=$this->job_announcement_model->selectLocations();
	 		$data["schedules"]=$this->job_announcement_model->selectSchedules();
	 		$this->load->template("templates/general","job_announcement/edit_announcement","Post Job Announcement",$data);
    	}else{
    		redirect(site_url("users"));
    	}
 	}
 	
 	public function save_changes(){
 	$Z_USER = Zsession::getUserFromSession();
 		if(isset($Z_USER["user_id"])&& $Z_USER["user_id"]!=""){
	    	$this->load->model("job_announcement_model");
	    	$this->load->helper('email');
	        $data=array();
	        $annid=$_POST['txtannid'];
	        $comid=$_POST['txtcomid'];
	        $name=trim($_POST['txtorgname']);
	        $desc=trim($_POST['txtdesc']);
	        $address=trim($_POST['txtaddress']);
	        $phone=trim($_POST['txtphone']);
	        $fax=trim($_POST['txtfax']);
	        $email=trim($_POST['txtemail']);
	        $contact=trim($_POST['txtcontact']);
	        $website=trim($_POST['txtwebsite']);
	        $notice=trim($_POST["txtappinfo"]);
	 		$closingdate=$_POST["txtclosingdate"];
			    /* Update Company */
	       		$this->job_announcement_model->updateCompany($comid,$name,$desc,$address,$phone,$fax,$email,$contact,$website);
				/* Update Announcement */
	       		$this->job_announcement_model->updateAnnouncement($annid,$notice,$closingdate);		
				/* Update Position Information */
 		 		$count=$_POST["number_of_titles"];
		 		for($i=1;$i<=$count;$i++){
		 			$this->job_announcement_model->updateCategory($_POST["title".$i],$_POST["cat".$i]);
		 			$this->job_announcement_model->updateLocation($_POST["title".$i],$_POST["loc".$i]);
		 			$this->job_announcement_model->updateSchedule($_POST["title".$i],$_POST["sched".$i]);
		 		}

	       		redirect(site_url("users/profiles?type=".Zencryption::encrypt(serialize("job_announcement"))));
 	   }else{
    		redirect(site_url("users"));
    	}
 	 	
 	}
 	public function delete_announcement($ann_id,$com_id){
 		$this->load->model("job_announcement_model");
 		$titles=$this->job_announcement_model->selectTitleByAnnounID($ann_id);
 		foreach ($titles->result_array() as $title){
 			$title_id=$title["title_id"];
 			$this->job_announcement_model->deleteScheduleByTitleId($title_id);
 			$this->job_announcement_model->deleteDutyByTitleId($title_id);
 			$this->job_announcement_model->deleteRequirementByTitleId($title_id);
 			$this->job_announcement_model->deleteLocationByTitleId($title_id);
 			$this->job_announcement_model->deleteCategoryByTitleId($title_id);
 		}
 		$this->job_announcement_model->deleteTitleByAnnounID($ann_id);
 		$this->job_announcement_model->deleteAnnouncement($ann_id);
 		$this->job_announcement_model->deleteCompany($com_id);
 		redirect(site_url("users/profiles?type=".Zencryption::encrypt(serialize("job_announcement"))));
 	}
 	
 	
 	/* JOB DISPLAY */
    function view_detail($ann_id){
    	$Z_USER=Zsession::getUserFromSession();
	    	$this->load->model("front_m_job");
	    	$this->load->model("job_announcement_model");
	    	$data=array();
	    	$this->front_m_job->addnew_hit_job($ann_id);
	    	$data['announcement']=$this->job_announcement_model->selectAnnouncement($ann_id);
	    	$data["titles"]=$this->job_announcement_model->selectTitleByAnnounID($ann_id);
	    	$this->load->template("templates/general","job_announcement/job_detail", "Job Announcement",$data);
    }
    function how_to_post(){
    	$Z_USER=Zsession::getUserFromSession();
    	if(isset($Z_USER["user_id"])){
    		redirect("job_announcement/post");
    	}else{
    		$this->load->template("templates/general","job_announcement/how_to_post", "Job Announcement");
    	}
    	
    }
	function job_list(){
    	$this->load->model("job_announcement_model");
    	$this->load->helper('url_helper');
    	$this->load->library('pagination');
    	$data =array();
    	$config=array();
    	$config['base_url'] = base_url().'index.php/job_announcement/job_list/';
      	$announcements=$this->job_announcement_model->selectAnnouncement(0);
      	$config['total_rows'] = count($announcements->result());
      	$config['per_page'] = '10';
      	$config['full_tag_open'] = '<p>';
      	$config['full_tag_close'] = '</p>';
      	$this->pagination->initialize($config);
      	
      	$data['categories']=$this->job_announcement_model->selectCategories();
      	$data['locations']=$this->job_announcement_model->selectLocations();
    	$data['announcements']=$this->job_announcement_model->selectAnnouncementRecord($config['per_page'],$this->uri->segment(3));
    	
    	$this->load->template("templates/general","job_announcement/job_list", "Job Announcement",$data);
    }
    
    function job_search_result(){
    	$this->load->model("job_announcement_model");
    	$this->load->helper('url_helper');
    	$this->load->library('pagination');
    	$data =array();
    	$config=array();
    	$category=trim($_POST['cbocategory']);
    	$location=trim($_POST['cbolocation']);
		$config['base_url'] = base_url().'index.php/job_announcement/job_search_result/';
      	$announcements=$this->job_announcement_model->selectJobByCategoryAndLocation($category,$location);
      	/*$config['total_rows'] = count($announcements->result());
      	$config['per_page'] = '5';
      	$config['full_tag_open'] = '<p>';
      	$config['full_tag_close'] = '</p>';
      	$this->pagination->initialize($config);*/
      	$data["cur_catid"]=$category;
      	$data["cur_locid"]=$location;
      	$data['categories']=$this->job_announcement_model->selectCategories();
      	$data['locations']=$this->job_announcement_model->selectLocations();
    	$data['announcements']=$this->job_announcement_model->selectJobByCategoryAndLocation($category,$location);
    	$this->load->template("templates/general","job_announcement/search_result", "Job Announcement",$data);
    }
 	/* END OF JOB DISPLAY */
}
?>